package ipdesk.model.report.design.action;

import ipdesk.model.common.dao.CommonDao;
import ipdesk.model.report.brand.dao.BrandInfoDao;
import ipdesk.model.report.design.bean.DesignInfoBean;
import ipdesk.model.report.design.dao.DesignInfoDao;
import ipdesk.model.report.promote.bean.PromoteInfoBean;
import ipdesk.model.report.promote.dao.PromoteInfoDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.FileService;
import ecg.util.StringUtils;
import ecg.util.Time;

public class DesignInfoRegAction extends ActionSupport implements Preparable, ModelDriven<DesignInfoBean>
{
	private DesignInfoBean designInfoBean;
	private DesignInfoBean designInfoDetail;
	private DesignInfoBean fileDetail;
	
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		String returnFlag = "SUCCESS";
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.design");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		designInfoBean.setPath_name(basePath);
		designInfoBean.setRegTime(regTime);
		//designInfoBean.setEmblem(StringUtils.nl2br(designInfoBean.getEmblem()));
		//designInfoBean.setSimilarDesign(designInfoBean.getSimilarDesign());
		//designInfoBean.setComment(designInfoBean.getComment());
		
		// desk
		if(!designInfoBean.getDc1().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc1()); }
		if(!designInfoBean.getDc2().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc2()); }
		if(!designInfoBean.getDc3().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc3()); }
		if(!designInfoBean.getDc4().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc4()); }
		if(!designInfoBean.getDc5().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc5()); }
		if(!designInfoBean.getDc6().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc6()); }
		
		int companySeqCount;
		
		if(designInfoBean.getEvaluationResult() == null){
			designInfoBean.setEvaluationResult(" ");
		}
		
		if(designInfoBean.getEvaluationResult().equals("a")){
			// 회사당 4개까지 등록확인
			companySeqCount = CommonDao.getInstance().companySeqCount(designInfoBean.getCompanySeq());
		}else {
			companySeqCount = 0;
		}
		
		
		if(companySeqCount > 4) {
			System.out.println("====4개이상");
			returnFlag = "COUNTFAIL";
		} else if(companySeqCount<4) {
			System.out.println("====4개이하");
			int lastDesignSeq = DesignInfoDao.getInstance().designInfoReg(designInfoBean);
		
			// 마지막으로 insert된 consult_seq 값을 빈 객체에 저장.		
			designInfoBean.setDesignSeq(lastDesignSeq);
			
			if(lastDesignSeq != 0 && designInfoBean.getUpload() != null)
			{
				String uploadFileName = designInfoBean.getUploadFileName();
				String realFileName = fileService.saveFile(designInfoBean.getUpload(), totalPath, uploadFileName);
				designInfoBean.setUploadFileName(realFileName);
				designInfoBean.setFile_category("FLE11");
				
				DesignInfoDao.getInstance().insertFileData(designInfoBean);
			}
			
			if(lastDesignSeq != 0 && designInfoBean.getUploadAF() != null)
			{
				String uploadFileName = designInfoBean.getUploadAFFileName();
				String realFileName = fileService.saveFile(designInfoBean.getUploadAF(), totalPath, uploadFileName);
				designInfoBean.setUploadFileName(realFileName);
				designInfoBean.setFile_category("FLE12");
				
				DesignInfoDao.getInstance().insertFileData(designInfoBean);
			}
			
			if(lastDesignSeq != 0 && designInfoBean.getUploadESC() != null)
			{
				String uploadFileName = designInfoBean.getUploadESCFileName();
				String realFileName = fileService.saveFile(designInfoBean.getUploadESC(), totalPath, uploadFileName);
				designInfoBean.setUploadFileName(realFileName);
				designInfoBean.setFile_category("FLE13");
				
				DesignInfoDao.getInstance().insertFileData(designInfoBean);
			}
			
			returnFlag = "SUCCESS";
		}
		
		return returnFlag;
	}
	
	public String completeRegForm() throws Exception
	{
		designInfoDetail = DesignInfoDao.getInstance().getDetailData(designInfoBean.getDesignSeq());
		
		return SUCCESS;
	}
	
	public String completeReg() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.design");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		designInfoBean.setPath_name(basePath);
		designInfoBean.setRegTime(regTime);
		
		DesignInfoDao.getInstance().insertCompleteData(designInfoBean);
		
		if(designInfoBean.getUpload() != null) {
			String uploadFileName = designInfoBean.getUploadFileName();
			String realFileName = fileService.saveFile(designInfoBean.getUpload(), totalPath, uploadFileName);
			designInfoBean.setUploadFileName(realFileName);
			designInfoBean.setFile_category("FLE10");
			
			DesignInfoDao.getInstance().insertFileData(designInfoBean);
		}
		
		return SUCCESS;
	}
	
	public String executeModifyForm() throws Exception
	{
		int designSeq = designInfoBean.getDesignSeq();
				
		designInfoDetail = DesignInfoDao.getInstance().getModifyData(designSeq); 
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE11");
		
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setDesign(fileDetail.getFile_name());
			designInfoDetail.setFileSeq1(fileDetail.getFile_seq());
		}
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE12");
		
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setApplicationForm(fileDetail.getFile_name());
			designInfoDetail.setFileSeq2(fileDetail.getFile_seq());
		}
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE13");
		
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setEvaluationScoreCard(fileDetail.getFile_name());
			designInfoDetail.setFileSeq3(fileDetail.getFile_seq());
		}
		
		return SUCCESS;
	}
	
	/**
	 * 파일 삭제 액션
	 * @return
	 */
	public String executeFileDelete() throws Exception
	{
		/**
		 * 1. 해당 designSeq의 path와 file name을 가지고 온다.
		 * 2. 테이블의 파일 데이터를 지운다.
		 * 3. 업로드 데이터를 지운다.
		 */
		String fileName = "";
		//boolean delFlag = DesignInfoDao.getInstance().fileDelete(designInfoBean.getDesignSeq());
		if(designInfoBean.getFileSeq1() != null) {
			designInfoBean.setFile_seq(designInfoBean.getFileSeq1());
			fileName = designInfoBean.getDesign();
		}
		
		if(designInfoBean.getFileSeq2() != null) {
			designInfoBean.setFile_seq(designInfoBean.getFileSeq2());
			fileName = designInfoBean.getApplicationForm();
		}
		
		if(designInfoBean.getFileSeq3() != null) {
			designInfoBean.setFile_seq(designInfoBean.getFileSeq3());
			fileName = designInfoBean.getEvaluationScoreCard();
		}
		
		if(designInfoBean.getFileSeq4() != null) {
			designInfoBean.setFile_seq(designInfoBean.getFileSeq4());
			fileName = designInfoBean.getNewDesign();
		}
		
		if(designInfoBean.getFile_seq() != null || !"".equals(designInfoBean.getFile_seq())) {
			String fileSeq = designInfoBean.getFile_seq();
			
			// 파일 있으면 파일부터 삭제
			String filePathPrefix = getText("path.upload.prefix");
			String pathName = designInfoBean.getPath_name();
			fileService.deleteFile(filePathPrefix + pathName + fileName);
			
			// 디비 파일삭제
			DesignInfoDao.getInstance().delFileData(fileSeq);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 홍보실적 수정 액션 처리
	 */
	public String executeModify() throws Exception
	{
		String returnFlag = "SUCCESS";
		/**
		 * 	1. 넘어온 컨설팅 번호를 조건으로 업로드된 데이터가 있는지 여부를 확인한다.
		 */
		int designSeq = designInfoBean.getDesignSeq();
		
		System.out.println("***"+designSeq);
		// 데이터 업데이트, 파일 업로드 처리.
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.design");
		String totalPath = uplodaPathPrefix + basePath;
		
		designInfoBean.setPath_name(basePath);
		
		String regTime = Time.getDateTime();
		designInfoBean.setRegTime(regTime);
		
		// desk 수정
		if(!designInfoBean.getDc1().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc1()); }
		if(!"".equals(designInfoBean.getDc2())) { designInfoBean.setDeskCode(designInfoBean.getDc2()); }
		if(!"".equals(designInfoBean.getDc3())) { designInfoBean.setDeskCode(designInfoBean.getDc3()); }
		if(!designInfoBean.getDc4().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc4()); }
		if(!designInfoBean.getDc5().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc5()); }
		if(!designInfoBean.getDc6().equals("")) { designInfoBean.setDeskCode(designInfoBean.getDc6()); }
		
		int companySeqCount;
		if(designInfoBean.getEvaluationResult() == null){
			designInfoBean.setEvaluationResult(" ");
		}
		// 회사당 4개까지 등록확인
		if(designInfoBean.getEvaluationResult().equals("a")){
			companySeqCount = CommonDao.getInstance().companySeqCount(designInfoBean.getCompanySeq());
		}else {
			companySeqCount = 0;
		}
		
		if(companySeqCount > 4) {
			System.out.println("====4개이상");
			returnFlag = "COUNTFAIL";
		} else if(companySeqCount<=4) {
			System.out.println("====4개이하");
			DesignInfoDao.getInstance().designInfoUpdate(designInfoBean);
			
			if(designInfoBean.getUpload() != null) {
				String uploadFileName = designInfoBean.getUploadFileName();
				String realFileName = fileService.saveFile(designInfoBean.getUpload(), totalPath, uploadFileName);
				designInfoBean.setUploadFileName(realFileName);
				designInfoBean.setFile_category("FLE11");
				
				DesignInfoDao.getInstance().insertFileData(designInfoBean);
			}
			
			if(designInfoBean.getUploadAF() != null) {
				String uploadFileName = designInfoBean.getUploadAFFileName();
				String realFileName = fileService.saveFile(designInfoBean.getUploadAF(), totalPath, uploadFileName);
				designInfoBean.setUploadFileName(realFileName);
				designInfoBean.setFile_category("FLE12");
				
				DesignInfoDao.getInstance().insertFileData(designInfoBean);
			}
			
			if(designInfoBean.getUploadESC() != null) {
				String uploadFileName = designInfoBean.getUploadESCFileName();
				String realFileName = fileService.saveFile(designInfoBean.getUploadESC(), totalPath, uploadFileName);
				designInfoBean.setUploadFileName(realFileName);
				designInfoBean.setFile_category("FLE13");
				
				DesignInfoDao.getInstance().insertFileData(designInfoBean);
			}
			returnFlag = "SUCCESS";
		}
	
		return returnFlag;
	}
	
	/**
	 * 완료보고서 수정폼
	 * @return
	 * @throws Exception
	 */
	public String completeModiForm() throws Exception {
		int designSeq = designInfoBean.getDesignSeq();
		
		designInfoDetail = DesignInfoDao.getInstance().getDetailData(designSeq);
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE10");
		
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setNewDesign(fileDetail.getFile_name());
			designInfoDetail.setFileSeq4(fileDetail.getFile_seq());
		}
		
		return SUCCESS;
	}
	
	/**
	 * 완료보고서 수정
	 * @return
	 * @throws Exception
	 */
	public String completeModi() throws Exception {
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.design");
		String totalPath = uplodaPathPrefix + basePath;
		
		designInfoBean.setPath_name(basePath);
		
		DesignInfoDao.getInstance().insertCompleteData(designInfoBean);
		
		if(designInfoBean.getUpload() != null) {
			String uploadFileName = designInfoBean.getUploadFileName();
			String realFileName = fileService.saveFile(designInfoBean.getUpload(), totalPath, uploadFileName);
			designInfoBean.setUploadFileName(realFileName);
			designInfoBean.setFile_category("FLE10");
			
			DesignInfoDao.getInstance().insertFileData(designInfoBean);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 데이터 삭제 액션
	 * 
	 */
	public String executeDelete() throws Exception
	{
		int designSeq = designInfoBean.getDesignSeq();

		// 1. 해당 글의 모든 첨부파일 삭제
		deleteFilePath(designSeq, "FLE10");
		deleteFilePath(designSeq, "FLE11");
		deleteFilePath(designSeq, "FLE12");
		deleteFilePath(designSeq, "FLE13");
				
		// 2. 글 삭제
		DesignInfoDao.getInstance().designInfoDelete(designSeq);

		return SUCCESS;
	}
	
	public void deleteFilePath(int designSeq, String category) throws Exception
	{
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, category);		
		if(fileDetail != null) {
			String filePathPrefix = getText("path.upload.prefix");
			String fileName = fileDetail.getFile_name();
			String pathName = fileDetail.getPath_name();
									
			fileService.deleteFile(filePathPrefix + pathName + fileName);
									
			String fileSeq = fileDetail.getFile_seq();
			DesignInfoDao.getInstance().delFileData(fileSeq);
		}
	}
	
	public DesignInfoBean getDesignInfoDetail()
	{
		return designInfoDetail;
	}

	public void setDesignInfoDetail(DesignInfoBean designInfoDetail)
	{
		this.designInfoDetail = designInfoDetail;
	}

	public void prepare() throws Exception
	{
		designInfoBean = new DesignInfoBean();
	}
	
	public DesignInfoBean getModel()
	{
		return designInfoBean;
	}
	
	public DesignInfoBean getFileDetail() {
		return fileDetail;
	}

	public void setFileDetail(DesignInfoBean fileDetail) {
		this.fileDetail = fileDetail;
	}

	/*public void validate()
	{
		String contentType = designInfoBean.getUploadContentType();
		long fileSize = 0;
		
		if(designInfoBean.getUpload() != null)
		{
			fileSize = designInfoBean.getUpload().length();
		}
		
		if(!fileService.contentTypeValidate(contentType))
		{
			addFieldError("upload", "업로드할 수 없는 파일형식입니다");
			return;
		}
		
		if(fileSize > Long.parseLong(getText("upload.size")))
		{
			addFieldError("upload", "업로드 용량을 초과하였습니다.");
			return;
		}
		
	}*/
}
